package active;

import com.alibaba.fastjson.JSON;
import pojo.AjaxResult;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月24日 14:34
 * @Description TODO
 */
@WebServlet("/test02/request")
public class Test02Ajax extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String paramA = req.getParameter("paramA");
        String paramB = req.getParameter("paramB");
        System.out.println("doGet");
        System.out.println( "请求参数："+paramA );
        System.out.println( "请求参数："+paramB );

        List<User> users = new ArrayList<>();
        users.add(new User(10001,"张三",0,"zhangsan","123456"));
        users.add(new User(10002,"tom",0,"zhangsan","123456"));
        users.add(new User(10003,"jack",0,"zhangsan","123456"));

        resp.setContentType("text/json;charset=utf-8");

        AjaxResult result = new AjaxResult("查询成功",true,users,100001,new Date());
        resp.getWriter().println(JSON.toJSONString( result ) );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String paramA = req.getParameter("paramA");
        String paramB = req.getParameter("paramB");
        System.out.println("doPost");
        System.out.println( "请求参数："+paramA );
        System.out.println( "请求参数："+paramB );

        List<User> users = new ArrayList<>();
        users.add(new User(10001,"张三",0,"zhangsan","123456"));

        resp.setContentType("text/json;charset=utf-8");

        AjaxResult result = new AjaxResult("查询成功",true,users,100001,new Date());
        resp.getWriter().println(JSON.toJSONString( result ) );
    }
}






